package com.bnzj.core.web.security.handler;

import org.springframework.security.core.Authentication;

import javax.validation.constraints.NotNull;
import java.util.LinkedList;
import java.util.Objects;

/**
 * @ClassName DelegatingAuthenticationParseFilter
 * @Description 聚合解析器，AuthenticationToken由有序集合解析
 * @Author
 * @Date 2020/3/3
 * @Version V1.0
 **/
public class DelegatingAuthenticationParseFilter implements AuthenticationParseFilter {

    /**
     * AuthenticationToken 解析集合
     */
    private LinkedList<AuthenticationParseFilter> delegates;

    public DelegatingAuthenticationParseFilter(@NotNull LinkedList<AuthenticationParseFilter> delegates){
        this.delegates = delegates;
    }

    @Override
    public String getUserName(Authentication authentication) {
        String username = null;
        for(AuthenticationParseFilter parse : delegates){
            username = parse.getUserName(authentication);
            if(!Objects.isNull(username)){
                break;
            }
        }
        return username;
    }
}
